package com.esm.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.esm.dao.DepartmentDao_lxy;
import com.esm.model.Department_lxy;
import com.esm.model.User;
import com.esm.utils.JDBCUtil;


/**
 * 这是部门dao层接口的实现
 * @author LiuXingYu
 *
 */
public class DepartmentDaoImpl_lxy implements DepartmentDao_lxy{
	@Override
	public int selectDeparmentCountBycourrentUid() {
		// TODO Auto-generated method stub
		int flag = 0;
		StringBuffer sql=new StringBuffer();
		sql.append("SELECT COUNT(dep_id) depcount ");
		sql.append(" FROM ems_dept");
		Connection con=JDBCUtil.getConnection();
		PreparedStatement pst=null;
		try {
			 pst=con.prepareStatement(sql.toString());
			ResultSet rs=pst.executeQuery();
			if(rs.next()){
				 flag=rs.getInt("depcount");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JDBCUtil.closeJDBC(pst, con);
		return flag;
	}

	@Override
	public List<Department_lxy> selectDeparments(int page, int rows) {
		// TODO Auto-generated method stub
		List<Department_lxy> Deparments=new ArrayList<Department_lxy>();
		StringBuffer sql=new StringBuffer();
		sql.append("SELECT dep_id,dep_name,manager FROM ems_dept ");
		sql.append(" limit ?,? ");
		Connection con=JDBCUtil.getConnection();
		PreparedStatement pst=null;
		try {
			pst=con.prepareStatement(sql.toString());
			pst.setInt(1, (page-1)*rows);
			pst.setInt(2, rows);
			ResultSet rs= pst.executeQuery();
			while(rs.next()){
				Department_lxy deparment= new Department_lxy();
				deparment.setDeptId(rs.getInt("dep_id"));
				deparment.setDeptName(rs.getString("dep_name"));
				deparment.setManager((rs.getString("manager")));
				Deparments.add(deparment);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JDBCUtil.closeJDBC(pst, con);
		return Deparments;
	}

	@Override
	public boolean updateDeparment(String deptName,int deptId,String manager,String currentUname) {
		// TODO Auto-generated method stub
		boolean  flag=false;
		StringBuffer sql=new StringBuffer();
		sql.append("UPDATE ems_dept SET dep_name =?,manager=? WHERE dep_id = ?");
		Connection con=JDBCUtil.getConnection();
		PreparedStatement pst=null;
		try {
			 pst=con.prepareStatement(sql.toString());
			pst.setString(1, deptName);
			pst.setString(2, manager);
			pst.setInt(3, deptId);
			boolean rs=pst.execute();
			if(rs){
				flag=true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		DepartmentCrodeImpl_lxy.getUpdateDepartmentCrode(deptName,manager,currentUname);
		JDBCUtil.closeJDBC(pst, con);
		return flag;
	}

	@Override
	public boolean deleteDepartment( int deptId,String deptName,String manager,String currentUname) {
		// TODO Auto-generated method stub
		boolean  flag=false;
		StringBuffer sql=new StringBuffer();
		sql.append("DELETE FROM ems_dept WHERE dep_id = ?");
		Connection con=JDBCUtil.getConnection();
		PreparedStatement pst=null;
		try {
			 pst=con.prepareStatement(sql.toString());
			pst.setInt(1, deptId);
			boolean rs=pst.execute();
			if(rs){
				flag=true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		DepartmentCrodeImpl_lxy.getDeleteDepartmentCrode(deptName,manager,currentUname);
		JDBCUtil.closeJDBC(pst, con);
		return flag;
	}

	@Override
	public boolean addDepartment(String deptName,String manager,String currentUname) {
		// TODO Auto-generated method stub
		boolean  flag=false;
		StringBuffer sql=new StringBuffer();
		sql.append("INSERT INTO ems_dept (dep_name,manager) VALUES (?,?)");
		Connection con=JDBCUtil.getConnection();
		PreparedStatement pst=null;
		try {
			 pst=con.prepareStatement(sql.toString());
			pst.setString(1, deptName);
			pst.setString(2, manager);
			boolean rs=pst.execute();
			if(rs){
				flag=true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		DepartmentCrodeImpl_lxy.getAddDepartmentCrode(deptName,manager,currentUname);
		JDBCUtil.closeJDBC(pst, con);
		return flag;
	}

}
